/**
 * 监测数据导入
 */
$(function(){
	var hospCode = paramTabPage[tabId].hospCode ;
	paramTabPage[tabId] = $("#tabContent > .tab-pane.active").data();//上一层页面传来的参数
	var date_now = new Date();
	if (date_now.getMonth()==2 && date_now.getDate()==30) {
		$('#doodles #date').html(date_now.getFullYear()+'年3月30日');
		$('#doodles').fadeIn(1000);
	}
	
	var year = new Date().getFullYear();
	$('#medicalRecordYear').val(year);
	
	$("#medicalRecordYear").on('change', function() {
		getMedicalRecordNum(hospCode);
	});
	
	$(".enable-month a").on('click', function() {
		var m = $(this).attr('data-month');
		var clazz = $(this).parent().attr('class');
		gotoMedicalRecordDetail(m,clazz);
	});
	
	getMedicalRecordNum(hospCode);
});

//获取某年各月首页数据条数
function getMedicalRecordNum(hospCode){
	var year = $('#medicalRecordYear').val();
	var currYear = new Date().getFullYear();
	var pageType="getMedicalRecordNum";
	if(paramTabPage[tabId].flag=="1"){
		pageType="getMedicalRecordYbNum";
	}
	//大于当前月份的标为不可用
	if (year>currYear) {
		$('.month').removeClass('enable-month');
	} else if(year==currYear){
		var currMonth = new Date().getMonth();
		currMonth +=1;
		for (var i = 1; i <currMonth; i++) {
			$('#'+i+'month').addClass('enable-month');
		}
		for (var i = currMonth; i <= 12; i++) {
			$('#'+i+'month').removeClass('enable-month');
		}
	} else{
		$('.month').addClass('enable-month');
	}
	
	var mrInfo={};
	var newIcon={};
	if (localStorage.mrInfo) {
		mrInfo = JSON.parse(localStorage.mrInfo);
	}
	if (localStorage.newIcon) {
		newIcon = JSON.parse(localStorage.newIcon);
	}
	for (var i = 1; i <= 12; i++) {
		if (i<10) {
			if (!mrInfo[year+'-0'+i]) {
				mrInfo[year+'-0'+i]=[0,new Date().getTime()];
			}
		} else if(!mrInfo[year+'-'+i]){
			mrInfo[year+'-'+i]=[0,new Date().getTime()];
		}
	}
	//获取某年各月首页数据条数
	$.post(contextPath +"/MedicalRecord/"+pageType+".dhc",{'year':year,'hospCode':hospCode},
		function(result){
		//初始化
		$('a[data-month]').html('0');
		$('.month .newIcon').fadeOut(20);
		var hasDataArray = new Array();
		//循环
		$.each(result,function(i){
			hasDataArray[i]=year+'-'+this.month;
			$('a[data-month="'+this.month+'"]').html(this.num);
			
			if (mrInfo[year+'-'+this.month]) {
				var time = new Date().getTime()-mrInfo[year+'-'+this.month][1];//相差时间
				var iconTime = new Date().getTime()-newIcon[year+'-'+this.month];//相差时间
				//判断，当上次该月记录的条数与本次不同且不等于0，并变化时间在1小时之内时添加NEW图标
				if (mrInfo[year+'-'+this.month][0]!=this.num) {
					if (this.num!=0 && time<3600000) {
						$('a[data-month="'+this.month+'"]').parent().find('.newIcon').fadeIn(20);//显示NEW图标
						newIcon[year+'-'+this.month]=new Date().getTime();
					}
					
					mrInfo[year+'-'+this.month]=[this.num,new Date().getTime()];
				}
				if (iconTime<300000) {
					$('a[data-month="'+this.month+'"]').parent().find('.newIcon').fadeIn(20);//显示NEW图标
				} else if(!isNaN(iconTime)){
					delete newIcon[year+'-'+this.month];//超过5分钟，不显示NEW图标
				}
			} else {//若不存在记录，则新增该月记录
				mrInfo[year+'-'+this.month]=[this.num,new Date().getTime()];
			}
		})
		
		//把没有数据的月置为空
		if(hasDataArray.length<12){
			for (var i = 1; i <= 12; i++) {
				var flag = true;
				var month = '';
				if (i<10) {
					month = year+'-0'+i;
				} else {
					month = year+'-'+i;
				}
				for ( var index in hasDataArray) {
					if (hasDataArray[index]== month) {
						flag = false;
					}
				}
				if (flag) {
					mrInfo[month]=[0,new Date().getTime()];
				}
			}
		}
		localStorage.mrInfo=JSON.stringify(mrInfo);//写入localStorage
		localStorage.newIcon=JSON.stringify(newIcon);//写入localStorage
		$('#'+tabId+' .icon-refresh').click(function(){
			autoQueryMR(hospCode);
		});
	});
}

//跳转到病例明细
function gotoMedicalRecordDetail(m,clazz) {
	if (clazz.indexOf('enable-month')<0) {
		return;
	}
	var year = $('#medicalRecordYear').val();
	var date = year+"-"+m;//时间
	if(paramTabPage[tabId].flag=="1"){
		openTab('/pages/yizheng/ywcHcCaseList.jsp',year+'年'+m+'月病例', year+'-'+m+'MedicalRecordHcDetail', year+'-'+m+'MedicalRecordHcDetail',{date:date});
	}else {
		openTab('/pages/yizheng/ywcCaseList.jsp', year+'年'+m+'月病例', year+'-'+m+'MedicalRecordDetail', year+'-'+m+'MedicalRecordDetail', {date:date});
	} 
}

//自动查询病例数据
function autoQueryMR(hospCode) {
	var mrInfo = JSON.parse(localStorage.mrInfo);
	var newIcon = JSON.parse(localStorage.newIcon);
	var queryNum = 0;
	var pageType = "getMedicalRecordNum";
	var year = $('#medicalRecordYear').val();
	for (var i = 1; i <= 12; i++) {
		if (i < 10) {
			if (!mrInfo[year + '-0' + i]) {
				mrInfo[year + '-0' + i] = [ 0, new Date().getTime() ];
			}
		} else if (!mrInfo[year + '-' + i]) {
			mrInfo[year + '-' + i] = [ 0, new Date().getTime() ];
		}
	}
	var hasDataArray = new Array();
	if (paramTabPage[tabId].flag == "1") {
		pageType = "getMedicalRecordYbNum";
	}

	$.ajax({
		type : "post",
		data : {
			'year' : year,
			'hospCode' : hospCode
		},
		url : contextPath + "/MedicalRecord/" + pageType + ".dhc",
		success : function(result) {
			// 初始化
			$('a[data-month]').html('0');
			$('.month .newIcon').fadeOut(20);
			// 循环
			$.each(result, function(i) {
				hasDataArray[i] = year + '-' + this.month;
				$('a[data-month="' + this.month + '"]').html(this.num);

				if (mrInfo[year + '-' + this.month]) {
					var time = new Date().getTime() - mrInfo[year + '-' + this.month][1];// 相差时间
					var iconTime = new Date().getTime() - newIcon[year + '-' + this.month];// 相差时间
					// 判断，当上次该月记录的条数与本次不同且不等于0，并变化时间在6秒之内时添加NEW图标和正在导入图标
					if (mrInfo[year + '-' + this.month][0] < this.num) {
						$('a[data-month="' + this.month + '"]').parent().find('.newIcon').fadeIn(20);// 显示NEW图标
						newIcon[year + '-' + this.month] = new Date().getTime();
						mrInfo[year + '-' + this.month] = [ this.num, new Date().getTime() ];
					}
					if (time < 6000) {
						$('a[data-month="' + this.month + '"]').parent().find('.importIcon').fadeIn();// 显示正在导入图标
					} else {
						$('a[data-month="' + this.month + '"]').parent().find('.importIcon').fadeOut();// 隐藏正在导入图标
					}
					if (iconTime < 300000) {
						$('a[data-month="' + this.month + '"]').parent().find('.newIcon').fadeIn(20);// 显示NEW图标
					} else if (!isNaN(iconTime)) {
						delete newIcon[year + '-' + this.month];// 超过5分钟，不显示NEW图标
					}
				} else {// 若不存在记录，则新增该月记录
					mrInfo[year + '-' + this.month] = [ this.num, new Date().getTime() ];
				}
			})
			// 把没有数据的月置为空
			if (hasDataArray.length < 12) {
				for (var i = 1; i <= 12; i++) {
					var flag = true;
					var month = '';
					if (i < 10) {
						month = year + '-0' + i;
					} else {
						month = year + '-' + i;
					}
					for ( var index in hasDataArray) {
						if (hasDataArray[index] == month) {
							flag = false;
						}
					}
					if (flag) {
						mrInfo[month] = [ 0, new Date().getTime() ];
					}
				}
			}
			localStorage.mrInfo = JSON.stringify(mrInfo);// 写入localStorage
			localStorage.newIcon = JSON.stringify(newIcon);// 写入localStorage
		},
		error : function(data) {
			$(document).ajaxStart(function() {
				layerload = drgUI.load(2);
			});
		}
	});
}